# encoding: utf-8

# 摩羯预测评估平台 Capevaluation 赛事预测(微服务接口)

module Capevaluation
  module Business
    module Prediction


      #curl http://127.0.0.1:5678/api -d cmd=predict_submit_predict -d type=0 -d player="FBA2017006" -d match_id=627318 -d play_code=001 -d prediction="3,0" -d gzip=0
      #curl http://127.0.0.1:5678/api -d cmd=predict_submit_predict -d type=1 -d player="13900312435" -d match_id=662386 -d play_code=001 -d prediction="3,1" -d gzip=0
      def predict_submit_predict(params)
        #self.logger.debug(CaiQiu::Support::Utils.get_backtrace_list)
        self.logger.debug(__method__.to_s + " params:#{params}")

        #1.检验参数是否为空
        if params['type'].nil? || params['player'].nil? || params['match_id'].nil? || params['play_code'].nil? || params['prediction'].nil?
          raise Capevaluation::Exception::LackOfParams.new
        end

        #2.验证参数有效性
        #验证type字段
        type = params['type'].to_i
        raise Capevaluation::Exception::InvalidParams.new if !type.zero? && type!=1


        #3.验证玩法 play_code, prediction
        check_play_items(params['play_code'], params['prediction'])


        #4.验证 match_id 是否未开场
        #football_match match_status == 0
        status = Capevaluation::Service::WbaiFootballMatch.is_match_begin_playing(params['match_id'])
        raise Capevaluation::Exception::MatchBeginning.new if status


        #.验证 play_odds 是否有效盘口
        #.亚盘, 盘口
        #.大小球, 盘口
        #.让球胜平负, 让球数
        #TODO...
        case params['play_code']
          when '001' then params['play_odds'] = nil
        end


        #5.add or update 记录
        if Capevaluation::Service::Submits.is_exists(params)
          Capevaluation::Service::Submits.update_prediction(params)
        else
          Capevaluation::Service::Submits.add_prediction(params)
        end

        []
      end



    end
  end
end

